package com.example.piece.tools

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.widget.Toast
import androidx.core.content.FileProvider
import com.example.piece.App
import java.io.File

object ShareUtil {

    const val PACKAGE_WECHAT = "com.tencent.mm"//微信
    const val PACKAGE_MOBILE_QQ = "com.tencent.mobileqq"//QQ
    const val PACKAGE_VB = "com.sina.weibo"//微博


    const val CLS_WECHAT = "com.tencent.mm.ui.tools.ShareImgUI"//微信
    const val CLS_WECHAT_FRIENDS = "com.tencent.mm.ui.tools.ShareToTimeLineUI"//朋友圈
    const val CLS_QQ = "com.tencent.mobileqq.activity.JumpActivity"//QQ
    const val CLS_VB = "com.sina.weibo.EditActivity"//微博


    /**
     * 分享文本到微信好友
     * @param context 上下文
     * @param content 分享内容
     */

    fun shareTextToWechat(context: Context, content: String) {
        if (checkAppInstalled(context, PACKAGE_WECHAT)) {
            val intent = Intent()
            val componentName = ComponentName(PACKAGE_WECHAT, CLS_WECHAT)
            intent.component = componentName
            intent.action = Intent.ACTION_SEND
            intent.putExtra(Intent.EXTRA_TEXT, content)
            intent.putExtra("Kdescription", "shareTextToWechat")
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)

        } else {
            Toast.makeText(context, "请先安装微信客户端", Toast.LENGTH_SHORT).show()
        }

    }

    /**
     * 分享图片到微信好友
     * @param context 上下文
     * @param files 分享图片
     */

    fun sharePictureToWechatFile(context: Context, files: List<File>, description: String) {
        if (checkAppInstalled(context, PACKAGE_WECHAT)) {
            val imageUris = ArrayList<Uri>()

            val intent = Intent()
            val componentName = ComponentName(PACKAGE_WECHAT, CLS_WECHAT)
            intent.component = componentName
            intent.action = Intent.ACTION_SEND_MULTIPLE
            intent.type = "image/*"
            for (file in files) {
                if (file != null && file.isFile && file.exists()) {
                    lateinit var fromFile: Uri

                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                        fromFile = Uri.fromFile(file)
                    } else {//7.0 以后Uri不能直接获取flie://xxx.jpg路径,
                        fromFile = FileProvider.getUriForFile(context,
                            context.packageName + ".fileProvider",
                            file);
                    }
                    imageUris.add(fromFile)
                }
            }
            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris)
            intent.putExtra("Kdescription", description)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)

        } else {
            Toast.makeText(context, "请先安装微信客户端", Toast.LENGTH_SHORT).show()
        }

    }

    /**
     * 分享图片到微信好友
     * @param context 上下文
     * @param Uri 分享图片
     */

    fun sharePictureToWechatUri(context: Context, uris: ArrayList<Uri>, description: String) {
        if (checkAppInstalled(context, PACKAGE_WECHAT)) {
            val imageUris = ArrayList<Uri>()
            imageUris.addAll(uris)
            val intent = Intent()
            val componentName = ComponentName(PACKAGE_WECHAT, CLS_WECHAT)
            intent.component = componentName
            intent.action = Intent.ACTION_SEND_MULTIPLE
            intent.type = "image/*"

            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris)
            intent.putExtra("Kdescription", description)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)

        } else {
            Toast.makeText(context, "请先安装微信客户端", Toast.LENGTH_SHORT).show()
        }

    }

    /**
     * 分享图片到微信朋友圈
     * @param context 上下文
     * @param files 分享图片
     */

    fun sharePictureToWechatFriendUri(context: Context, uris: ArrayList<Uri>, description: String) {
        if (checkAppInstalled(context, PACKAGE_WECHAT)) {
            val imageUris = ArrayList<Uri>()
            imageUris.addAll(uris)
            val intent = Intent()
            val componentName = ComponentName(PACKAGE_WECHAT, CLS_WECHAT_FRIENDS)
            intent.component = componentName
            intent.type = "image/*"



            if (App.isHightWX) {
                intent.action = Intent.ACTION_SEND
                intent.putExtra(Intent.EXTRA_STREAM, imageUris[0])
            } else {
                intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris)
                intent.action = Intent.ACTION_SEND_MULTIPLE


            }
            intent.putExtra("Kdescription", description)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)

        } else {
            Toast.makeText(context, "请先安装微信客户端", Toast.LENGTH_SHORT).show()
        }

    }

    /**
     * 分享图片到微信朋友圈
     * @param context 上下文
     * @param files 分享图片
     */

    fun sharePictureToWechatFriend(context: Context, files: List<File>, description: String) {
        if (checkAppInstalled(context, PACKAGE_WECHAT)) {
            val imageUris = ArrayList<Uri>()

            val intent = Intent()
            val componentName = ComponentName(PACKAGE_WECHAT, CLS_WECHAT_FRIENDS)
            intent.component = componentName
            intent.type = "image/*"
            for (file in files) {
                if (file != null && file.isFile && file.exists()) {
                    lateinit var fromFile: Uri

                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                        fromFile = Uri.fromFile(file)
                    } else {//7.0 以后Uri不能直接获取flie://xxx.jpg路径,
                        fromFile = FileProvider.getUriForFile(context,
                            context.packageName + ".fileProvider",
                            file);
                    }
                    imageUris.add(fromFile)
                }
            }

            if (App.isHightWX) {
                intent.action = Intent.ACTION_SEND
                intent.putExtra(Intent.EXTRA_STREAM, imageUris[0])
            } else {
                intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris)
                intent.action = Intent.ACTION_SEND_MULTIPLE


            }
            intent.putExtra("Kdescription", description)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)

        } else {
            Toast.makeText(context, "请先安装微信客户端", Toast.LENGTH_SHORT).show()
        }

    }

    /**
     * 分享文本到QQ好友
     * @param context 上下文
     * @param content 分享内容
     */

    fun shareTextToQQFriend(context: Context, content: String) {
        if (checkAppInstalled(context, PACKAGE_MOBILE_QQ)) {
            val intent = Intent()
            val componentName = ComponentName(PACKAGE_MOBILE_QQ, CLS_QQ)
            intent.component = componentName
            intent.type = "text/plain"
            intent.action = Intent.ACTION_SEND
            intent.putExtra(Intent.EXTRA_TEXT, content)
            intent.putExtra(Intent.EXTRA_SUBJECT, "分享")
            intent.putExtra("Kdescription", "shareTextToQQFriend")
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)

        } else {
            Toast.makeText(context, "请先安装QQ客户端", Toast.LENGTH_SHORT).show()
        }

    }

    /**
     * 分享图片到QQ好友
     * @param context 上下文
     * @param files 分享图片
     */

    fun sharePictureToQQFriend(context: Context, files: List<File>, description: String) {
        if (checkAppInstalled(context, PACKAGE_MOBILE_QQ)) {
            val imageUris = ArrayList<Uri>()

            val intent = Intent()
            val componentName = ComponentName(PACKAGE_MOBILE_QQ, CLS_QQ)
            intent.component = componentName
            intent.action = Intent.ACTION_SEND_MULTIPLE

            intent.type = "image/*"
            for (file in files) {
                if (file != null && file.isFile && file.exists()) {
                    lateinit var fromFile: Uri

                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                        fromFile = Uri.fromFile(file)
                    } else {//7.0 以后Uri不能直接获取flie://xxx.jpg路径,
                        fromFile = FileProvider.getUriForFile(context,
                            context.packageName + ".fileProvider",
                            file);
                    }
                    imageUris.add(fromFile)
                }
            }
            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris)
            intent.putExtra("Kdescription", description)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)

        } else {
            Toast.makeText(context, "请先安装QQ客户端", Toast.LENGTH_SHORT).show()
        }

    }

    /**
     * 分享文本到QQ好友
     * @param context 上下文
     * @param content 分享内容
     */

    fun shareTextToVB(context: Context, content: String) {
        if (checkAppInstalled(context, PACKAGE_VB)) {
            val intent = Intent()
            val componentName = ComponentName(PACKAGE_VB, CLS_VB)
            intent.component = componentName
            intent.type = "text/plain"
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.action = Intent.ACTION_MAIN;

            intent.action = Intent.ACTION_SEND
            intent.putExtra(Intent.EXTRA_TEXT, content)
            intent.putExtra(Intent.EXTRA_SUBJECT, "分享")
            intent.putExtra("Kdescription", "shareTextToVB")
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)

        } else {
            Toast.makeText(context, "请先安装新浪微博客户端", Toast.LENGTH_SHORT).show()
        }

    }

    /**
     * 分享图片到微博
     * @param context 上下文
     * @param files 分享图片
     */

    fun sharePictureToVB(context: Context, files: List<File>, description: String) {
        if (checkAppInstalled(context, PACKAGE_VB)) {
            val imageUris = ArrayList<Uri>()

            val intent = Intent()
            val componentName = ComponentName(PACKAGE_VB, CLS_VB)
            intent.component = componentName
            intent.action = Intent.ACTION_SEND_MULTIPLE
            intent.type = "image/*"
            for (file in files) {
                if (file != null && file.isFile && file.exists()) {
                    lateinit var fromFile: Uri

                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                        fromFile = Uri.fromFile(file)
                    } else {//7.0 以后Uri不能直接获取flie://xxx.jpg路径,
                        fromFile = FileProvider.getUriForFile(context,
                            context.packageName + ".fileProvider",
                            file);
                    }
                    imageUris.add(fromFile)
                }
            }
            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris)
            intent.putExtra("Kdescription", description)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)

        } else {
            Toast.makeText(context, "请先安装新浪微博客户端", Toast.LENGTH_SHORT).show()
        }

    }


    /**
     * 判断是否安装App
     * @param context 上下文
     * @param packageName 包名
     */

    fun checkAppInstalled(context: Context, packageName: String): Boolean {
        if (packageName.isNullOrEmpty()) {
            return false
        }

        val packageManager = context.packageManager
        val installedPackages = packageManager.getInstalledPackages(0)
        if (installedPackages.isNullOrEmpty()) {
            return false
        }

        for (pkg in installedPackages) {

            if (packageName == pkg.packageName) {
                return true
            }
        }

        return false

    }
}